import {cbs, phoneReg, pwdReg} from '../../../../config/settings.js';
import md5 from 'js-md5';
import SendCode from '../../../Common/SendCode/';

module.exports = {
    name: 'password',
    data(){
        return{ 
            rules: {
                oldPwd: [{
                    validator: (rule, value, cb) => {
                        if(this.getRouteId === 'account') {
                            if(value === '') {
                                cb(new Error('请输入旧密码'));
                            }else if(!pwdReg.test(value)) {
                                cb(new Error('密码长度需在6-16位之间，同时包含数字和字母'));
                            }else {
                                cb();
                            }
                        }else {
                            cb();
                        }
                    },
                    trigger: 'blur'
                }],
                phone: [{
                    validator: (rule, value, cb) => {
                        if(this.getRouteId === 'pay') {
                            if(value === '') {
                                cb(new Error('请输入已绑定的手机号'));
                            }else if(!phoneReg.test(value)){
                                cb(new Error('请输入正确的手机号'));
                            }else {
                                cb();
                            }
                        }else {
                            cb();
                        }
                    },
                    trigger: 'blur'
                }],
                code: [{
                    validator: (rule, value, cb) => {
                        if(this.getRouteId === 'account') {
                            if(value === '') {
                                cb(new Error('请输入验证码'));
                            }else {
                                cb();
                            }
                        }else {
                            cb();
                        }
                    },
                    trigger: 'blur'
                }],
                newPwd: [{
                    validator: (rule, value, cb) => {
                        if(value === '') {
                            cb(new Error('请输入新密码'));
                        }else if(!pwdReg.test(value)) {
                            cb(new Error('密码长度需在6-16位之间，同时包含数字和字母'));
                        }else if(value === this.form.oldPwd) {
                            cb(new Error('新旧密码不能一样'));
                        }else {
                            if(this.form.conPwd !== '') {
                                this.$refs.form.validateField('conPwd')
                            }
                            cb();
                        }
                    },
                    trigger: 'blur'
                }],
                conPwd: [{
                    validator: (rule, value, cb) => {
                        if(value === '') {
                            cb(new Error('请再次输入密码'));
                        }else if(value !== this.form.newPwd) {
                            cb(new Error('两次输入密码不一致'));
                        } else {
                            cb();
                        }
                    },
                    trigger: 'blur'
                }]
            },
            form: {
                newPwd: '',
                conPwd: ''
            },
            // 设置支付密码
            setPayForm: {
                pwd: '',
                conPwd: ''
            },
            setPayRules: {
                pwd: [{
                    validator: (rule, value, cb) => {
                        if(value === '') {
                            cb(new Error('请输入密码'));
                        }else if(!pwdReg.test(value)) {
                            cb(new Error('密码长度需在6-16位之间，同时包含数字和字母'));
                        }else {
                            cb();
                        }
                    },
                    trigger: 'blur'
                }],
                conPwd: [{
                    validator: (rule, value, cb) => {
                        if(value === '') {
                            cb(new Error('请再次输入密码'));
                        }else if(value !== this.setPayForm.pwd) {
                            cb(new Error('两次输入密码不一致'));
                        } else {
                            cb();
                        }
                    },
                    trigger: 'blur'
                }]
            },
            // 忘记支付密码
            valiPhoneForm: {
                phone: '',
                code: ''
            },
            valiPhoneRules: {
                phone: [{
                    validator: (rule, value, cb) => {
                        if(value === '') {
                            cb(new Error('请输入已绑定的手机号'));
                        }else if(!phoneReg.test(value)){
                            cb(new Error('请输入正确的手机号'));
                        }else {
                            cb();
                        }
                    },
                    trigger: 'blur'
                }],
                code: [{
                    validator: (rule, value, cb) => {
                        if(value === '') {
                            cb(new Error('请输入验证码'));
                        }else {
                            cb();
                        }
                    },
                    trigger: 'blur'
                }],
            },
            // 新支付密码
            newPayForm: {
                pwd: '',
                conPwd: ''
            },
            newPayRules: {
                pwd: [{
                    validator: (rule, value, cb) => {
                        if(value === '') {
                            cb(new Error('请输入密码'));
                        }else if(!pwdReg.test(value)) {
                            cb(new Error('密码长度需在6-16位之间，同时包含数字和字母'));
                        }else {
                            cb();
                        }
                    },
                    trigger: 'blur'
                }],
                conPwd: [{
                    validator: (rule, value, cb) => {
                        if(value === '') {
                            cb(new Error('请再次输入密码'));
                        }else if(value !== this.newPayForm.pwd) {
                            cb(new Error('两次输入密码不一致'));
                        } else {
                            cb();
                        }
                    },
                    trigger: 'blur'
                }]
            },
            btnIsDisabled: false,
            payBtnIsDisabled: false,
            remindPayPwd: false,
            validatePhone: true,
        }
    },
    methods: {
        /**
         * 重设一项
         */
        reset(name, formName="form") {
            this[formName][name] = '';
        },
        /**
         * 重设所有项
         */
        resetAll(name) {
            this.$refs[name].resetFields();
        },
        // reminder() {
        //     this.dialog.show = true;
        // },
        /**
         * 判断提交类型
         */
        submit(name) { 
            this.getRouteId === 'pay' ? this.userinfo.hasPay ? this.modifyPayPwd(name) : this.setPayPwd(name) : this.modifyAccountPwd(name);
        },
        /**
         * 修改账号密码
         */
        modifyAccountPwd(name) {
            this.$refs[name].validate(valid => {
                if(valid) {
                    this.btnIsDisabled = true;

                    let data = this.$$formData({
                        oldPassword: md5(this[name].oldPwd),
                        newPassword: md5(this[name].newPwd)
                    })

                    this.$$api_manager_editPwd(data, res => {
                        if(res.event === 0) {
                            this.$store.dispatch('remove_userinfo').then(() => {
                              cbs.submitSuccess.call(this, '修改成功，请重新登录');
                              this.$router.push('/login');
                          })
                        }else if(res.event === 4) {
                            cbs.submitSuccess.call(this, '密码格式错误');
                        }else {
                            cbs.submitError.call(this, `修改失败, ${res.msg}`);
                        }
                        this.btnIsDisabled = false;
                    })
                }else {
                    cbs.submitError.call(this, `修改失败`);
                    this.btnIsDisabled = false;
                }
            })
        },
        /**
         * 设置支付密码
         */
        setPayPwd(name) {
            this.payBtnIsDisabled = true;

            this.$refs[name].validate(valid => {
                if(valid) {
                    let data = this.$$formData({
                        payPassword: md5(this[name].pwd)
                    }) 

                    this.$$api_manager_setPayPwd(data, res => {
                        if(res.event === 0) {
                            this.$router.push('/account/list');
                            this.changePayStatus(true);
                        }else {
                            cbs.submitError.call(this, `设置失败, ${res.msg}`);
                        }
                        this.payBtnIsDisabled = false
                    })
                }else {
                    cbs.submitError.call(this, `设置失败`);
                    this.payBtnIsDisabled = false;
                }
            })
        },
        /**
         * 修改用户密码状态
         */
        changePayStatus(flag) {
            this.$store.state.user.userinfo.hasPay = flag;
        },
        /**
         * 通过原密码修改支付密码
         */
        modifyPayPwd(name) {

            this.$refs[name].validate(valid => {
                if(valid) {
                    this.btnIsDisabled = true;

                    let data = this.$$formData({
                        oldPayPassword: md5(this[name].oldPwd),
                        newPayPassword: md5(this[name].newPwd)
                    })

                    this.$$api_manager_editPayPwd(data, res => {
                        if(res.event === 0) {
                          cbs.submitSuccess.call(this, '修改成功');
                          this.btnIsDisabled = false;
                          this.$router.push('/account/list');
                        }else {
                            cbs.submitError.call(this, `修改失败, ${res.msg}`);
                        }
                        this.btnIsDisabled = false;
                    })
                }else {
                    cbs.submitError.call(this, `修改失败`);
                    this.btnIsDisabled = false;
                }
            })
        },
        /**
         * 忘记支付密码
         */
        forgetPayPwd() {
            this.remindPayPwd = true;
            this.validatePhone = true;
        },
        /**
         * 忘记支付密码-获取验证码后下一步
         */
        setPayNext(name) {
            this.$refs[name].validate(valid => {
                if(valid) {
                    this.validatePhone = false;
                }
            })
        },
        /**
         * 通过验证码修改支付密码
         */
        modifyNewPayPwd(name) {
            this.payBtnIsDisabled = true;

            this.$refs[name].validate(valid => {
                if(valid) {
                    let data = this.$$formData({
                        phone: this.valiPhoneForm.phone,
                        verificationCode: this.valiPhoneForm.code,
                        payPassword: md5(this.newPayForm.pwd)
                    })

                    this.$$api_manager_modPwdByCode(data, res => {
                        if(res.event === 0) {
                            cbs.submitSuccess.call(this, '修改成功');
                            this.$router.push('/account/list');
                            this.remindPayPwd = false;
                        }else if(res.event === -1) {
                            cbs.submitError.call(this, `修改失败，验证码错误`);
                        }else {
                            cbs.submitError.call(this, `修改失败，${res.msg}`);
                        }
                        this.validatePhone = true;
                    })
                }
                this.payBtnIsDisabled = false;
            })
        },
        /**
         * 设置dialog宽度
         */
        setDialogSize(width) {
            $('.dialog-pay .el-dialog').css('width', width);
        },
        handleChange() {
            this.$refs.code.changeStatus(0);
        }
    },
    created() {
    },
    computed: {
        getTitle() {
            return this.getRouteId === 'pay' ? this.userinfo.hasPay ? '修改支付密码' : '设置支付密码' : '修改账号密码';
        },
        getRouteId() {
            let id = this.$route.params.id;
            // id === 'pay' ? this.switchToPayForm() : this.switchToAccForm();
            return id;
        },
        userinfo() {
            return this.$store.state.user.userinfo;
        }
    },
    components: {
        SendCode
    },
    mounted() {
        this.setDialogSize('340px');
    }
}